package neuroBot.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import neuroBot.bot.party.Main;
import neuroBot.exploit.party.FrameHandler;

public class PokerFrame {
	public static JFrame frame;
	public static JPanel mainPane;
	public static JTextArea infoTA = new JTextArea("los" + "\n");
	public static JScrollPane jcp;
	public static JButton stopButton = new JButton("Stop");
	public static JButton pauseButton = new JButton("Pause");
	public static MyOutPutStream outStream = new MyOutPutStream();
	public static MyPrintStream printStream = new MyPrintStream();

	public static void start() {
		if (Main.isDebugOutputFile)
			DebugFileWriter.start();
		frame = new JFrame("Debug-Info");
		frame.setPreferredSize(new Dimension(360, 520));
		frame.setLocation(410, 490);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setResizable(false);
		// scrollPane = new JPanel();
		mainPane = new JPanel();
		ActionListener action_stop = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("PokerBot stopped by User");
				System.exit(0);
			}
		};
		stopButton.addActionListener(action_stop);
		ActionListener action_pause = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Main.pause = !Main.pause;
				if (Main.pause) {
					append("Pause\n");
					pauseButton.setText("Continue");
				} else {
					append("Continue\n");
					pauseButton.setText("Pause");
				}
			}
		};
		pauseButton.addActionListener(action_pause);
		mainPane.add(stopButton, BorderLayout.NORTH);
		mainPane.add(pauseButton, BorderLayout.NORTH);
		infoTA.setPreferredSize(new Dimension(335, 460));
		infoTA.setEditable(true);
		// infoTA.setBackground(Color.lightGray);
		// scrollPane.add(infoTA, BorderLayout.CENTER);
		jcp = new JScrollPane(infoTA);
		jcp.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL));
		jcp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		jcp
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jcp.setAutoscrolls(true);
		jcp.setWheelScrollingEnabled(true);
		jcp.setPreferredSize(new Dimension(345, 470));
		mainPane.add(jcp);
		frame.setContentPane(mainPane);
		frame.pack();
		frame.setVisible(true);
		// frame.setAlwaysOnTop(true);
	}

	public static void append(String s) {
		PokerFrame.infoTA.append(s);
		PokerFrame.infoTA.setPreferredSize(new Dimension(335, PokerFrame.infoTA
				.getLineCount() * 16));
		PokerFrame.infoTA.revalidate();
		PokerFrame.infoTA
				.setCaretPosition(PokerFrame.infoTA.getText().length());
		if (Main.isDebugOutputFile)
			DebugFileWriter.print(s);
	}

	public static void toBack() {
		if (frame != null) {
			frame.toBack();
//			if (frame.isActive()) {
//				new FrameHandler(Main.pokerFrameNr).hidePBDebugWin();
//			}
			// frame.setVisible(false);
		}
	}

	public static void toFront() {
		if (frame != null) {
			frame.toFront();
			frame.requestFocus();
//			if (!frame.isActive()) {
//				new FrameHandler(Main.pokerFrameNr).activatePBDebugWin();
//			}
			// frame.setVisible(true);
		}
	}
}

class MyOutPutStream extends OutputStream {
	public void write(int n) {
		PokerFrame.append("" + n);
	}
}

class MyPrintStream extends PrintStream {
	public MyPrintStream() {
		super(PokerFrame.outStream);
	}

	public void println(String s) {
		PokerFrame.append(s + "\n");
	}

	public void print(String s) {
		PokerFrame.append(s);
	}

	public void print(int i) {
		PokerFrame.append("" + i);
	}

	public void println(int i) {
		PokerFrame.append("" + i + "\n");
	}

	public void print(boolean b) {
		PokerFrame.append("" + b);
	}

	public void println(boolean b) {
		PokerFrame.append("" + b + "\n");
	}

	public void print(Object o) {
		PokerFrame.append(o.toString());
	}

	public void println(Object o) {
		PokerFrame.append(o.toString() + "\n");
	}
}
